import requests, json, os, time
import multiprocessing as mp
from bs4 import BeautifulSoup as Soup


def downloader(appnumber):
	appnum = appnumber[2:]
	year = appnum[:4]
	# writing directory
	wrdir = "{}".format(year)
	# Posting Accesstoken from EPO
	url = "https://ops.epo.org/3.2/auth/accesstoken"
	payload = "grant_type=client_credentials"
	headers = {
	    'Content-Type': "application/x-www-form-urlencoded",
	    'Authorization': 'Basic IDkzSmtSMnJBTXhnU0ZqaENPSU1VVEJhdUltSFVra3VTOmpmY0U0OWxNNkFIbnZxS2I=',
	    'Accept': "*/*",
	    'Cache-Control': "no-cache",
	    'Host': "ops.epo.org",
	    'accept-encoding': "gzip, deflate",
	    'content-length': "29",
	    'Connection': "keep-alive",
	    'cache-control': "no-cache"
	    }
	while True:
		try:
			response = requests.request("POST", url, data=payload, headers=headers, timeout = (10, 30))
			break
		except:
			pass
	time.sleep(0.3)
	response = response.text
	response_json  = {}
	if response != "":
		response_json = json.loads(response)
	token = response_json["access_token"]
	# Getting Equivalents for each application number
	url = "http://ops.epo.org/3.2/rest-services/published-data/application/docdb/KR.{}.A/equivalents".format(appnum)
	headers = {
	    'Content-Type': "application/x-www-form-urlencoded",
	    'Authorization': "Bearer "+token ,
	    'Accept': "*/*",
	    'Cache-Control': "no-cache",
	    'Host': "ops.epo.org",
	    'accept-encoding': "gzip, deflate",
	    'content-length': "29",
	    'Connection': "keep-alive",
	    'cache-control': "no-cache"
	    }
	while True:
		try:
			response = requests.request("GET", url, data=payload, headers=headers, timeout = (10,30))
			break
		except:
			pass
	time.sleep(0.3)
	xml = response.content
	# In case of ambiguous request
	soup = Soup(response.text, "xml")
	try:
		code = soup.find("code").text
	except:
		code = None
	if code == "CLIENT.AmbiguousRequest":
		resolutions = soup.find_all("resolution")
		for i, resolution in enumerate(resolutions):
			if resolution.get_text()[-2:]=="B1":
				new = resolution.get_text()
		if new[-2:] == "B1":
			url = "http://ops.epo.org/3.2/rest-services/published-data/"+new+"/equivalents"
			while True:
				try:
					response = requests.request("GET", url, data=payload, headers=headers, timeout = (10,30))
					xml = response.content
					break
				except:
					pass
	# Creating Directory
	try:	
		os.mkdir(wrdir)
	except:
		pass
	# Writing File
	file = wrdir + "\\" + appnum + ".xml"
	with open(file, "wb") as f:
		f.write(xml)
	size = os.path.getsize(file)
	size = round(size/1024.0, 1)
	print("{} - {}KB".format(appnum,size))

def main(xml):
	soup = Soup(xml, 'xml')
	appnumber = soup.find("applicationNumber").text
	appnumber = appnumber.replace("-", "")
	downloader(appnumber)



# Multi-Processing Mapping
if __name__ == '__main__':
	pool = mp.Pool(50)
	for year in range(1948, 2018):
		file = open("D:\\KIPRIS\\Biblio\\xml\\{}.xml".format(year), "r", encoding="utf-8")
		file = file.read()
		xml_list = file.split('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>')[1:]
		pool.map(main, xml_list)
	